package ua.od.maxz;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * User: ua.od.maxz
 * Date: Jun 22, 2010
 * Time: 1:58:51 PM
 */
public class KillerU extends Killer {
    @Override
    protected String getCommandForTasksList() {
        return "ps -ef";
    }

    @Override
    protected Collection<Integer> getPids(String processList) {
        String[] lines = processList.split("\n");
        Set<Integer> ret = new HashSet<Integer>();
        for (String line : lines) {
            if(line.contains("Bootstrap")) {
                int i = line.indexOf(" ");
                if(i!=-1) {
                    do {
                        i++;
                        if(line.charAt(i)!=' ') {
                            break;
                        }
                    } while(true);
                }
                int iNextSpace = line.indexOf(" ", i);
                String pid = line.substring(i, iNextSpace).trim();
                ret.add(Integer.parseInt(pid));
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        new KillerU().getPids("user   58926     9 50 17:41 pts/1    00:00:51 /home/user/soft/jdk1.6.0_24/bin/java -Djava.util.logging.config.file=/ccm/trunk/tomcat-dist/conf/logging.properties -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=CCM-OOMDump.hprof -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms128m -Xmx400m -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n -Djava.endorsed.dirs=/ccm/trunk/tomcat-dist/endorsed -classpath :/ccm/trunk/tomcat-dist/bin/bootstrap.jar:/ccm/trunk/tomcat-dist/conf:/ccm/trunk/tomcat-dist/bin/tomcat-juli.jar -Dcatalina.base=/abc/trunk/tomcat-dist -Dcatalina.home=/ccm/trunk/tomcat-dist -Djava.io.tmpdir=/abc/trunk/tomcat-dist/temp org.apache.catalina.startup.Bootstrap start");
    }

    @Override
    protected String getKillCommand() {
        return "kill -9 ";
    }
}
